using UnityEngine;
using UnityEngine.UI;

namespace DL.Tools
{
    public class FPSDisplay : MonoBehaviour
    {
        public bool OpenFPS = true;
        [SerializeField] private float m_UpdateInterval = 1f;
        // [SerializeField] private Text m_FrameraText;
        
        private float m_Timer;
        private float m_AvarageFrame;
        
        private GUIStyle m_Style;

        void Start()
        {
            DontDestroyOnLoad(gameObject);
            m_Style = new GUIStyle();
            m_Style.fontSize = 14;
        }

        void Update()
        {
            if(!OpenFPS) return;
            
            var smoothDeltaTime = Time.smoothDeltaTime;
            if (m_Timer <= 0)
            {
                m_Timer = m_UpdateInterval;
                m_AvarageFrame = 1 / smoothDeltaTime; 
                //
            }
            else
            {
                m_Timer -= smoothDeltaTime;
            }

        }


        private void OnGUI()
        {
            if(!OpenFPS) return;
            GUI.Label(new Rect(0, 0, 400, 400), string.Format("FPS:{0}", m_AvarageFrame.ToString("F2")), m_Style);
        }
        
        
    }
}
